package com.modules.system.controller;

import com.common.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.common.base.PageBean;
import com.common.base.PageResult;
import com.common.base.Return;
import com.common.util.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.modules.system.entity.SysRoleEntity;
import com.modules.system.service.SysRoleMenuService;
import com.modules.system.service.SysRoleService;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseController {
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;

	/**
	 * 角色列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@RequiresPermissions("sys:role:list")
	public Return list(@RequestParam Map<String, Object> params){
		//查询列表数据
		PageBean get = new PageBean(params);
		Page<SysRoleEntity> page = PageHelper.startPage(get.getPageIndex(),get.getPageSize());
		String roleName = BeanUtil.getMapStringValue(params, "rolaName");
		SysRoleEntity param = new SysRoleEntity();
		param.setRoleName(roleName);
		sysRoleService.getList(param);
		return Return.success().put(PageResult.setResult(page));
	}

	/**
	 * 角色列表
	 */
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	@RequiresPermissions("sys:role:select")
	public Return select(){
		List<SysRoleEntity> list = sysRoleService.getList(null);
		return Return.success().put(list);
	}

	/**
	 * 角色信息
	 */
	@RequestMapping(value = "/info/{roleId}", method = RequestMethod.GET)
	@RequiresPermissions("sys:role:info")
	public Return info(@PathVariable("roleId") Long roleId){
		SysRoleEntity role = sysRoleService.getByPrimaryKey(roleId);

		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.getMenuIdList(roleId);
		role.setMenuIdList(menuIdList);

		return Return.success().put(role);
	}

	/**
	 * 保存角色
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@RequiresPermissions("sys:role:save")
	public Return save(@RequestBody SysRoleEntity role){

		sysRoleService.save(role);

		return Return.success();
	}

	/**
	 * 修改角色
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@RequiresPermissions("sys:role:update")
	public Return update(@RequestBody SysRoleEntity role){

		sysRoleService.update(role);

		return Return.success();
	}

	/**
	 * 删除角色
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	@RequiresPermissions("sys:role:delete")
	public Return delete(@RequestBody Long[] roleIds){
		sysRoleService.deleteBatch(roleIds);

		return Return.success();
	}
}
